package com.ruoyi.project.app.controller;


import ai.advance.cmp.api.client.bean.ResponseBean;
import com.ruoyi.project.app.service.AppMyService;
import com.ruoyi.project.app.util.XException;
import com.ruoyi.project.app.util.XResult;
import com.ruoyi.project.beautyflower.BfUtil;
import com.ruoyi.project.beautyflower.bankCard.domain.BankCard;
import com.ruoyi.project.beautyflower.bankCard.service.BankCardServiceImpl;
import com.ruoyi.project.beautyflower.loanSetting.domain.LoanSetting;
import com.ruoyi.project.beautyflower.loanSetting.service.LoanSettingServiceImpl;
import com.ruoyi.project.beautyflower.member.domain.Member;
import com.ruoyi.project.beautyflower.member.service.IMemberService;
import com.ruoyi.project.beautyflower.userBankcard.domain.UserBankcard;
import com.ruoyi.project.beautyflower.userBankcard.service.UserBankcardServiceImpl;
import com.ruoyi.project.beautyflower.userProtocol.domain.UserProtocol;
import com.ruoyi.project.beautyflower.userProtocol.service.UserProtocolServiceImpl;
import com.ruoyi.project.system.dict.service.IDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户公共模块
 */
@RestController
@RequestMapping("/app/my")
@Api(tags = "用户公共模块")
public class AppMyController {

    @Autowired
    private AppMyService appMyService;

    @Autowired
    private IMemberService memberService;//用户service

    @Autowired
    private LoanSettingServiceImpl loanSettingService;//贷款设置

    @Autowired
    private IDictDataService dictDataService;//字典数据查询

    @Autowired
    private BankCardServiceImpl bankCardService;//银行

    @Autowired
    private UserProtocolServiceImpl userProtocolService;//用户协议

    @Autowired
    private UserBankcardServiceImpl userBankcardService;


    /**
     * 用户实名认证
     * @return
     */
    @PostMapping("selectProductList")
    @ApiOperation(notes = "selectProductList", value = "用户实名认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户真实姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "carId", value = "用户身份证号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "String")
    })
    public Object selectProductList(@RequestParam @ApiIgnore Map<String, String> params)  {
        int count =appMyService.setNameAuthentication(params);
        if(count>0){
            return new XResult("认证成功",count);
        }else{
            return new XResult("认证失败");
        }
    }


    /**
     * 查询借款用途设置列表
     * @return
     */
    @PostMapping("selectLoanPurposeList")
    @ApiOperation(notes = "selectLoanPurposeList", value = "查询借款用途列表")
    public Object selectLoanPurposeList()  {
        return new XResult("查询成功", appMyService.selectLoanPurposeList());
    }

    /**
     * 查询分期月数设置列表
     * @return 分期月数设置集合
     */
    @PostMapping("selectStagingMonthsList")
    @ApiOperation(notes = "selectStagingMonthsList", value = "查询分期月数列表")
    public Object selectStagingMonthsList()  {
        return new XResult("查询成功", appMyService.selectStagingMonthsList());
    }



    /**
     * 获取银行列表
     * @return 获取银行列表集合
     */
    @PostMapping("getBankCodeList")
    @ApiOperation(notes = "getBankCodeList", value = "获取银行列表")
    public Object getBankCodeList()  {
//        List<BankCode> bankCodeList= Lists.newArrayList();
//        for(BankCodeEnum bankCodeEnum : BankCodeEnum .values()){
//            BankCode bankCode=new BankCode();
//            bankCode.setCode(bankCodeEnum.getCode());
//            bankCode.setDesc(bankCodeEnum.getName());
//            bankCodeList.add(bankCode);
//        }


        return new XResult("获取成功", bankCardService.selectBankCardList(new BankCard()));
    }

    /**
     * 用户绑定银行卡预授权申请
     * @param params
     * @return
     */
    @PostMapping("requestBindCardAuthDemo")
    @ApiOperation(notes = "requestBindCardAuthDemo", value = "银行卡绑定预授权申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "backCardName", value = "银行名称", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "backCardNumber", value = "银行卡号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "String")
//            @ApiImplicitParam(name = "userName", value = "真实姓名", required = true, paramType = "query", dataType = "String"),
//            @ApiImplicitParam(name = "mobileNumber", value = "银行卡预留手机号", required = true, paramType = "query", dataType = "String"),
//            @ApiImplicitParam(name = "idNumber", value = "银行卡身份证件号码", required = true, paramType = "query", dataType = "String")
    })
    public Object requestBindCardAuthDemo(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        Member member=memberService.selectMemberById(Integer.parseInt(params.get("userId")));
        if(member!=null){
            params.put("userName",member.getName());
            params.put("mobileNumber",member.getMobile());
            params.put("idNumber",member.getCarId());
        }else{
            throw  new XException("用户不存在");
        }
        UserBankcard userBankcard1=new UserBankcard();
        userBankcard1.setBankCardNumber(params.get("backCardNumber"));
//        userBankcard1.setUserId(Integer.valueOf(params.get("userId")));
        List<UserBankcard> userBankcardList=userBankcardService.selectUserBankcardList(userBankcard1);
        if(userBankcardList!=null&&userBankcardList.size()>0){
            throw new XException("该银行卡已绑定");
        }
        ResponseBean responseBean=BfUtil.requestBindCardAuthDemo(params);
        if(responseBean.getCode().equals("SUCCESS")){
            UserBankcard userBankcard2=userBankcardService.selectUserBankcardByBankCardNumber(params.get("backCardNumber"),"0");
            if(userBankcard2!=null){
                userBankcardService.updateUserBankcard(userBankcard2);
            }else{
                UserBankcard userBankcard=new UserBankcard();
                userBankcard.setBankCardName(params.get("backCardName"));
                userBankcard.setBankCardNumber(params.get("backCardNumber"));
                userBankcard.setUserId(Integer.valueOf(params.get("userId")));
                userBankcard.setStatus(0);
                userBankcardService.insertUserBankcard(userBankcard);
            }
            return new XResult("操作成功",responseBean);
        }else{
            return new XResult("操作失败",responseBean);
        }
    }
    /**
     * 用户绑定银行卡预授权确认
     * @param params
     * @return
     */
    @PostMapping("insertBackCard")
    @ApiOperation(notes = "insertBackCard", value = "用户绑定银行卡预授权确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "backCardName", value = "银行名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "backCardNumber", value = "银行卡号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "cardAuthNo", value = "" +
                    "", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "authProtocolNo", value = "授权码（协议号）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "verificationCode", value = "验证码", required = true, paramType = "query", dataType = "String")
    })
    public Object insertBackCard(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("绑定成功", appMyService.insertBackCard(params));
    }

    /**
     * 查询用户个人信息
     * @param params
     * @return
     */
    @PostMapping("selectUserById")
    @ApiOperation(notes = "selectUserById", value = "查询用户个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string")
    })
    public Object selectUserById(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", memberService.selectMemberById(Integer.parseInt(params.get("userId"))));
    }
    /**
     * 查询用户个人绑定的银行卡
     * @param params
     * @return
     */
    @PostMapping("selectUserBankcardByUserId")
    @ApiOperation(notes = "selectUserBankcardByUserId", value = "查询用户个人绑定的银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string")
    })
    public Object selectUserBankcardByUserId(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", appMyService.selectUserBankcardByUserId(params.get("userId")));
    }
    /**
     * 用户借款申请记录添加
     * @param params
     * @return
     */
    @PostMapping("insertLoanApplication")
    @ApiOperation(notes = "insertLoanApplication", value = "用户借款申请记录添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string")
    })
    public Object insertLoanApplication(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", appMyService.insertLoanApplication(params.get("userId")));
    }


    /**
     * 借款审核结果
     * @param params
     * @return
     */
    @PostMapping("auditResults")
    @ApiOperation(notes = "auditResults", value = "借款审核结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string")
    })
    public Object auditResults(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", appMyService.auditResults(params.get("userId")));
    }
    /**
     * 保存用户头像
     * @param params
     * @return
     */
    @PostMapping("updateHeadImg")
    @ApiOperation(notes = "updateHeadImg", value = "保存用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "logo_url", value = "头像地址", required = true, paramType = "query", dataType = "string")
//            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query", dataType = "string"),
//            @ApiImplicitParam(name = "sex", value = "性别", required = true, paramType = "query", dataType = "string"),
//            @ApiImplicitParam(name = "barthDate", value = "出生年月", required = true, paramType = "query", dataType = "string")
    })
    public Object updateHeadImg(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("保存成功", appMyService.updateHeadImg(params));
    }
    /**
     * 用户删除银行卡
     * @param params
     * @return
     */
    @PostMapping("deleteUserBankcardById")
    @ApiOperation(notes = "deleteUserBankcardById", value = "用户删除银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "back_id", value = "银行卡Id(多条根据逗号分隔)", required = true, paramType = "query", dataType = "String"),
    })
    public Object deleteUserBankcardById(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        int count =0;
        count =appMyService.deleteUserBankcardById(params.get("back_id"));
        if(count>0){
            return new XResult("删除成功",count);
        }
        return new XResult("删除失败");
    }




    /**
     * 最高贷款设置
     * @param params
     * @return
     */
    @PostMapping("selectLoanSettingList")
    @ApiOperation(notes = "selectLoanSettingList", value = "最高贷款设置")
    public Object selectLoanSettingList(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", loanSettingService.selectLoanSettingList(new LoanSetting()).get(0));
    }


    /**
     * 申请期限设置
     * @param params
     * @return
     */
    @PostMapping("getTermType")
    @ApiOperation(notes = "getTermType", value = "申请期限设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "期限类型(1：申请额度成功期限2：申请额度失败期限)", required = true, paramType = "query", dataType = "string"),
    })
    public Object getTermType(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        if(params.get("type").equals("1")){
            return new XResult("查询成功", dictDataService.selectDictDataByType("term_type_success"));
        }else{
            return new XResult("查询成功", dictDataService.selectDictDataByType("term_type_error"));
        }
    }
    /**
     * 联系人关系数据查询
     * @param params
     * @return
     */
    @PostMapping("getRelationshipList")
    @ApiOperation(notes = "getRelationshipList", value = "联系人关系数据查询")
    public Object getRelationshipList(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", dictDataService.selectDictDataByType("relationship_type"));
    }
    /**
     * 用户协议查询
     * @param params
     * @return
     */
    @GetMapping("selectUserProtocolList")
    @ApiOperation(notes = "selectUserProtocolList", value = "用户协议查询")
    public Object selectUserProtocolList(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        List<UserProtocol> userProtocols=userProtocolService.selectUserProtocolList(new UserProtocol());
        return userProtocols.get(0).getContent();
    }
    /**
     * 更新在线审核时间
     * @param params
     * @return
     */
    @PostMapping("updateLoanApplication")
    @ApiOperation(notes = "updateLoanApplication", value = "更新在线审核时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "date", value = "在线审核时间", required = true, paramType = "query", dataType = "string")
//            ,
//            @ApiImplicitParam(name = "type", value = "申请状态", required = true, paramType = "query", dataType = "string")
    })
    public Object updateLoanApplication(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
        return new XResult("查询成功", appMyService.updateLoanApplication(params.get("userId"),params.get("date"),""));
    }
//    /**
//     * 更新申请时间
//     * @param params
//     * @return
//     */
//    @PostMapping("updateApplyDate")
//    @ApiOperation(notes = "updateApplyDate", value = "更新申请时间")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
//            @ApiImplicitParam(name = "type", value = "申请状态", required = true, paramType = "query", dataType = "string")
//    })
//    public Object updateApplyDate(@RequestParam @ApiIgnore Map<String, String> params, @ApiIgnore HttpServletRequest request)  {
//        return new XResult("更新成功", appMyService.updateApplyDate(params.get("userId"),params.get("type")));
//    }
}
